package com.mf.ergate.service.auth.impl;

import com.mf.ergate.api.auth.model.dto.RoleResourceRelationDeleteDto;
import com.mf.ergate.api.auth.model.dto.RoleResourceRelationQueryDto;
import com.mf.ergate.dao.auth.RoleResourceRelationDao;
import com.mf.ergate.domain.model.auth.RoleResourceRelationEntity;
import com.mf.ergate.service.GenericService;
import com.mf.ergate.service.auth.RoleResourceRelationService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色与资源关联Service层实现类
 * User: zhaoming
 * DateTime: 2019-02-08
 * To change this template use File | Settings | File Templates.
 **/
@Service
public class RoleResourceRelationServiceImpl extends GenericService<RoleResourceRelationEntity> implements RoleResourceRelationService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private RoleResourceRelationDao roleResourceRelationDao;

    /**
     * 保存角色与菜单关联关系
     * @param roleResourceRelationEntities 角色与资源关联Entity列表
     */
    @Transactional
    public Boolean save(List<RoleResourceRelationEntity> roleResourceRelationEntities){
        // 删除旧数据
        RoleResourceRelationQueryDto roleResourceRelationQueryDto = new RoleResourceRelationQueryDto();
        roleResourceRelationQueryDto.setRoleNo(roleResourceRelationEntities.get(0).getRoleNo());
        List<RoleResourceRelationEntity> roleResourceRelationList = roleResourceRelationDao.selectByCondition(roleResourceRelationQueryDto);
        if (CollectionUtils.isNotEmpty(roleResourceRelationList)) {
            roleResourceRelationDao.deleteByUniqueNos(roleResourceRelationList);
        }
        // 批量插入
        return roleResourceRelationDao.batchInsert(roleResourceRelationEntities);
    }
}
